import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'survivalManageM',

  state: {
    loading1: false,
    idList: [], //存储id列表
  },

  effects: {
    //智能拆分的下拉
    *getProductByType({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getProductByType, payload);

      if (response) {
        if (response.code == 0) {
          callback(response.data);
        }
      }
    },

    //保存拆分结果
    *saveHandalSplitTrade({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.saveHandalSplitTrade, payload);
      if (response) {
        if (response.code == 0) {
          callback(response);
          message.success('拆分成功');
        } else {
          message.error(response.msg || response.message || '拆分失败');
        }
      } else {
        message.error('拆分失败');
      }
    },

    //智能拆分的表格数据
    *splitTrade({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.splitTrade, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data);
        } else {
          callback();
          message.error(response.msg || '获取智能拆分表格数据失败 ');
        }
      } else {
        callback();
        message.error('获取智能拆分表格数据失败');
      }
    },

    //获取交易信息
    *getTradeList({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getTradeList, payload);
      if (response) {
        if (response.code == 0) {
          setTimeout(() => {
            callback('成功', response.data);
          }, 3000);
        } else {
          callback('失败');
          message.error(response.msg || '查询交易信息列表失败!');
        }
      } else {
        callback('失败');
        message.error('查询交易信息列表失败!');
      }
    },

    //获取拆分后的交易流水
    *getTransactionFlow({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getSplitTradeList, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data);
        } else {
          callback('失败');
          message.error(response.msg || '查询拆分后的交易流水失败!');
        }
      } else {
        callback('失败');
        message.error('查询拆分后的交易流水失败!');
      }
    },

    //回退流水
    *rollbackSplitTrade({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.rollbackSplitTrade, payload);
      if (response) {
        if (response.code == 0) {
          message.success('回退成功');
          callback(response);
        } else {
          message.error(response.message || response.msg || '回退失败');
        }
      } else {
        message.error('回退失败');
      }
    },

    //删除流水
    *deleteFlow({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.deleteFlow, payload);

      if (response) {
        if (response.code == 0) {
          message.success('删除成功');
          callback(response);
        } else {
          message.error('删除失败');
        }
      } else {
        message.error('删除失败');
      }
    },

    //查看产品下拉列表
    *queryProductIdList({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.queryProductList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data.dataSource || []);
        } else {
          callback('fail');
          message.error(response.msg || '查询产品下拉列表失败!');
        }
      } else {
        callback('fail');
        message.error('查询产品下拉列表失败!');
      }
    },
    //查看已分配资产列表
    *queryAssignedAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.initialSplit.queryAssignedAsset, payload);

      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '查询已分配资产失败!');
        }
      } else {
        callback('fail');
        message.error('查询已分配资产失败!');
      }
    },

    //试算
    *tryCalc({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.tryCalc, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '试算失败!');
        }
      } else {
        callback('fail');
        message.error('试算失败!');
      }
    },

    //确认转仓
    *confirmAdust({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.confirmAdust, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'save',
            payload: {
              idList: [], //存储id列表
            },
          });
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '转仓失败!');
        }
      } else {
        callback('fail');
        message.error('转仓失败!');
      }
    },
    //内部转仓 智能转仓
    *smartTrans({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.smartTrans, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data ? response.data.dataSource || {} : {});
        } else {
          callback();
          message.error(response.msg || '智能转仓失败!');
        }
      } else {
        callback();
        message.error('智能转仓转仓失败!');
      }
    },
    //查询系列产品的全部数据
    *queryAllProduct({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.queryAllProduct, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data || {};
          callback(data);
        } else {
          callback();
          message.error(response.msg || '产品持仓查询失败!');
        }
      } else {
        callback();
        message.error('产品持仓查询失败!');
      }
    },
    //整体转仓接口
    *totalTransfer({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.totalTransfer, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data ? response.data.dataSource : []);
        } else {
          callback();
          message.error(response.msg || '整体转仓试算失败!');
        }
      } else {
        callback();
        message.error('整体转仓试算失败!');
      }
    },
    //真实交易拆分弹窗里的产品代码下拉
    *getProductListByListId({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getProductListByListId, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data ? response.data : []);
        } else {
          callback();
          message.error(response.msg || '查询产品代码下拉失败!');
        }
      } else {
        callback();
        message.error('查询产品代码下拉失败!');
      }
    },
    //获取交易流水的下载类型
    *getExportTypeList({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getExportTypeList, payload);
      if (response) {
        if (response.code == 0) {
          callback(response.data ? response.data : []);
        } else {
          callback();
          message.error(response.msg || '获取下载类型失败!');
        }
      } else {
        callback();
        message.error('获取下载类型失败!');
      }
    },

    //下载前获取token
    *saveToken({ payload, callback }, { call, put }) {
      const response = yield call(api.saveToken, payload);
      if (response) {
        callback(response);
      } else {
        callback();
      }
    },
    //获取该真实流水是多资产还是单资产
    *getAssetType({ payload, callback }, { call, put }) {
      const response = yield call(api.survivalManage.getAssetType, payload);
      // const response={
      //   code:0,
      //   data:0,
      //   msg:''
      // }
      if (response) {
        callback(response);
      } else {
        callback();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
